package org.exez.modules.system.controller;


import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.exez.common.api.vo.Result;
import org.exez.common.system.query.QueryGenerator;
import org.exez.common.system.vo.LoginUser;
import org.exez.common.util.oConvertUtils;
import org.exez.modules.system.entity.SysLog;
import org.exez.modules.system.entity.SysRole;
import org.exez.modules.system.entity.SysUser;
import org.exez.modules.system.service.ISysLogService;
import org.exez.modules.system.service.ISysUserService;
import org.exez.modules.system.vo.SysLogVO;
import org.exez.modules.visitor.constant.AuthorityEnum;
import org.exez.modules.visitor.entity.ReservationApplication;
import org.exez.modules.visitor.entity.WhiteList;
import org.exez.modules.visitor.util.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 系统日志表 前端控制器
 * </p>
 *
 * @Author zhangweijian
 * @since 2018-12-26
 */
@Api(tags = "系统日志")
@RestController
@RequestMapping("/sys/log")
@Slf4j
public class SysLogController {

	@Autowired
	private ISysLogService sysLogService;

	@Autowired
	private ISysUserService userService;

	/**
	 * @功能：查询日志记录
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value = "查询日志记录", notes = "查询日志记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "operateType", value = "2添加，3更新，4删除", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "页码大小", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "username", value = "操作用户名称", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "institutionId", value = "单位id", required = false, dataType = "String", paramType = "query"),
	})
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<IPage<SysLogVO>>  queryPageList(@RequestParam(name="operateType",required = false) Integer operateType,
											   @RequestParam(name="username",required = false) String username,
											   @RequestParam(name="institutionId",required = false) Integer institutionId,
											   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		Result<IPage<SysLogVO>> result = new Result<>();

		LambdaQueryWrapper<SysLog> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysLog :: getLogType,3);

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		if (null != institutionId) {
			queryWrapper.eq(SysLog::getInstitutionId, institutionId);
		}else if (!AuthorityEnum.ONE_USER.getIndex().equals(sysUser.getAuthority())) {
			queryWrapper.in(SysLog::getInstitutionId, AES.integerList(sysUser.getInstitutionId()));
		}

		if (operateType != null) {
			queryWrapper.eq(SysLog :: getOperateType,operateType);

		}
		if (StringUtils.isNotBlank(username)) {
			queryWrapper.like(SysLog :: getUsername,username);
		}
		queryWrapper.orderByDesc(SysLog::getCreateTime);

		Page<SysLog> page = new Page<>(pageNo, pageSize);

		IPage<SysLogVO> sysLogVOIPage = sysLogService.listLog(page, queryWrapper);


		result.setSuccess(true);
		result.setResult(sysLogVOIPage);
		return result;
	}

	/**
	 * @功能：删除单个日志记录
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public Result<SysLog> delete(@RequestParam(name="id",required=true) String id) {
		Result<SysLog> result = new Result<SysLog>();
		SysLog sysLog = sysLogService.getById(id);
		if(sysLog==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = sysLogService.removeById(id);
			if(ok) {
				result.success("删除成功!");
			}
		}
		return result;
	}

	/**
	 * @功能：批量，全部清空日志记录
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
	public Result<SysRole> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<SysRole> result = new Result<SysRole>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			if("allclear".equals(ids)) {
				this.sysLogService.removeAll();
				result.success("清除成功!");
			}
			this.sysLogService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}


}
